OPC Studio User's Guide and Reference
OPC Wizard Communication Objects
Fundamentals > OPC Wizard Fundamentals > OPC Wizard Data Model > OPC Wizard Communication Objects
In This Topic

The EasyUAServer Object

The OPC Wizard concentrates the actual communication functionality that makes up an OPC UA Server in the EasyUAServer Class.

Your code uses the properties and methods on this object to define the parameters and the contents of the server (see OPC Wizard Node Objects), and its behavior.

You will normally create and start just one instance of the EasyUAServer Class in your program. Although OPC Wizard can handle multiple server objects at the communication level, doing so without extra precautions can create a conflict on the OPC UA application level when such servers are started, because they share common application identification parameters in the OPC UA system, such as the Application URI.

Server Object Configuration Properties

The EasyUAServer Class has various properties that can be used to influence how the server works. The most important ones are:

Some server object properties can be pre-set using the object constructor (see below). However, you can always create the server object empty (using the default constructor), and then set its properties by assigning to them. 

Effective Endpoint Descriptor

If you want to obtain the OPC UA endpoint descriptor of the EasyUAServer, you can retrieve it using the EffectiveEndpointDescriptor Property. The endpoint descriptor can be used with QuickOPC for communication with the server (note that in essence, the endpoint URL is sufficient, but the endpoint descriptor also contains the message security mode and other information that can narrow down the OPC UA endpoint selection).

The following example illustrates how you can retrieve the effective endpoint descriptor, and use it in OPC UA client code.

Example: Examples - Server OPC UA - Obtain effective endpoint descriptor

Constructing the Server Object

An instance of the EasyUAServer Class is normally created by calling its constructor. The default constructor (with no parameters) creates the server objects with all defaults. There are various constructor overloads that allow you to initialize the newly created server object with certain most important properties (such as the endpoint URLs and message security modes) upfront. The various ways of constructing the server object are illustrated in the examples below, and in the examples listed in the "See Also" section at the bottom of the page..

A common way of constructing the EasyUAServer is to use the constructor that takes the URL of the server endpoint as an argument (this is illustrated in the example code further below).

If you are targeting .NET Framework and certain project types (such as Windows Forms), the EasyUAServer component will appear in the Visual Studio Toolbox. Dragging it from the Toolbox to the designer surface automatically creates a code to construct the server object, and a member variable to hold the new instance. The Properties window then provides means to configure the server object directly from the Visual Studio designer. In the end, the code created by the Visual Studio designer is equivalent to writing the code manually - it consists of calling the default constructor, and then assigning the new property values, where they differ from the defaults.

.NET

// This example shows different ways of constructing the EasyUAServer object.
// You can use any OPC UA client, including our Connectivity Explorer and OpcCmd utility, to connect to the server. 
//
// Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
// OPC client, server and subscriber examples in C# on GitHub: https://github.com/OPCLabs/Examples-OPCStudio-CSharp .
// Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
// a commercial license in order to use Online Forums, and we reply to every post.

using System;
using OpcLabs.EasyOpc.UA;
using OpcLabs.EasyOpc.UA.Engine;
using OpcLabs.EasyOpc.UA.NodeSpace;

namespace UAServerDocExamples._EasyUAServer
{
    class _Construction
    {
        public static void Main1()
        {
            // The toolkit provides a ready-made shared instance of the server object which you can use without even having
            // to construct it. Not recommended for use in library code, because it is a shared instance, and its usage may
            // therefore conflict with other code using the same instance.
            var server0 = EasyUAServer.SharedInstance;


            // The simplest way to construct the server object is to use the default constructor. The server will run on its
            // default endpoint URL "opc.tcp://localhost:48040/".
            var server1 = new EasyUAServer();


            // The server object can be constructed with a specific single endpoint URL string passed as an argument to the
            // constructor.
            var server2 = new EasyUAServer("opc.tcp://localhost:38444");


            // The server object can also be constructed with multiple endpoint URL strings passed as an array to the
            // constructor.
            var server3 = new EasyUAServer(new []
            {
                "opc.tcp://localhost:38444", "opc.tcp://localhost:38445", "opc.tcp://localhost:38446"
            });


            // If the language supports variable number of arguments (such as C# or VB.NET), the multiple endpoint URL
            // strings can be passed to it as separate arguments, instead of having to create an array of them.
            var server4 = new EasyUAServer(
                "opc.tcp://localhost:38444", "opc.tcp://localhost:38445", "opc.tcp://localhost:38446");


            // The server object can be constructed with specific message security modes.
            var server5 = new EasyUAServer(UAMessageSecurityModes.Secure);


            // The message security modes can be combined with the endpoint URL string.
            var server6 = new EasyUAServer(UAMessageSecurityModes.Secure, "opc.tcp://localhost:38444");


            // The message security modes can also be combined with multiple endpoint URL strings in an array.
            var server7 = new EasyUAServer(UAMessageSecurityModes.Secure, new[]
            {
                "opc.tcp://localhost:38444", "opc.tcp://localhost:38445", "opc.tcp://localhost:38446"
            });


            // If the language supports variable number of arguments (such as C# or VB.NET), the message security modes can
            // be combined with multiple endpoint URL strings passed to it as separate arguments, instead of having to create
            // an array of them.
            var server8 = new EasyUAServer(
                UAMessageSecurityModes.Secure, 
                "opc.tcp://localhost:38444", "opc.tcp://localhost:38445", "opc.tcp://localhost:38446");


            // The endpoint can be specified using the Uri object.
            var server9 = new EasyUAServer(new Uri("opc.tcp://localhost:38444"));


            // The server object can also be constructed with multiple Uri objects for server endpoints, passed as an array
            // to the constructor.
            var server10 = new EasyUAServer(new[]
            {
                new Uri("opc.tcp://localhost:38444"), 
                new Uri("opc.tcp://localhost:38445"), 
                new Uri("opc.tcp://localhost:38446")
            });


            // If the language supports variable number of arguments (such as C# or VB.NET), the multiple endpoint Uri
            // objects can be passed to it as separate arguments, instead of having to create an array of them.
            var server11 = new EasyUAServer(
                new Uri("opc.tcp://localhost:38444"),
                new Uri("opc.tcp://localhost:38445"),
                new Uri("opc.tcp://localhost:38446")
            );


            // The message security modes can be combined with the endpoint Uri object.
            var server12 = new EasyUAServer(UAMessageSecurityModes.Secure, new Uri("opc.tcp://localhost:38444"));


            // The message security modes can also be combined with multiple endpoint Uri objects in an array.
            var server13 = new EasyUAServer(UAMessageSecurityModes.Secure, new[]
            {
                new Uri("opc.tcp://localhost:38444"),
                new Uri("opc.tcp://localhost:38445"),
                new Uri("opc.tcp://localhost:38446")
            });


            // If the language supports variable number of arguments (such as C# or VB.NET), the message security modes can
            // be combined with multiple endpoint Uri objects passed to it as separate arguments, instead of having to create
            // an array of them.
            var server14 = new EasyUAServer(
                UAMessageSecurityModes.Secure,
                new Uri("opc.tcp://localhost:38444"),
                new Uri("opc.tcp://localhost:38445"),
                new Uri("opc.tcp://localhost:38446")
            );


            // The message security modes and the endpoint URL string can be set after the server object is constructed.
            var server15 = new EasyUAServer();
            server15.MessageSecurityModes = UAMessageSecurityModes.Secure;
            server15.EndpointUrlString = "opc.tcp://localhost:38444";


            // If the language supports property initializers (such as C# or VB.NET), the above code can be written more
            // concisely.
            var server16 = new EasyUAServer
            {
                MessageSecurityModes = UAMessageSecurityModes.Secure,
                EndpointUrlString = "opc.tcp://localhost:38444"
            };


            // If the language supports collection initializers (such as C# or VB.NET), the server object can be constructed
            // with the contents of the Objects folder, such as the data variables, in a single statement.
            var server17 = new EasyUAServer
            {
                new UADataVariable("Constant1").ConstantValue(42),
                new UADataVariable("Constant2").ConstantValue("abc")
            };
        }
    }
}
' This example shows different ways of constructing the EasyUAServer object.
' You can use any OPC UA client, including our Connectivity Explorer and OpcCmd utility, to connect to the server. 
'
' Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
' OPC client and subscriber examples in VB.NET on GitHub: https://github.com/OPCLabs/Examples-QuickOPC-VBNET .
' Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
' a commercial license in order to use Online Forums, and we reply to every post.

Imports System
Imports Opc.Ua
Imports OpcLabs.EasyOpc.UA
Imports OpcLabs.EasyOpc.UA.Engine
Imports OpcLabs.EasyOpc.UA.NodeSpace

Namespace _EasyUAServer
    Partial Friend Class _Construction
        Shared Sub Main1()
            ' The toolkit provides a ready-made shared instance of the server object which you can use without even having
            ' to construct it. Not recommended for use in library code, because it is a shared instance, and its usage may
            ' therefore conflict with other code using the same instance.
            Dim server0 = EasyUAServer.SharedInstance


            ' The simplest way to construct the server object is to use the default constructor. The server will run on its
            ' default endpoint URL "opc.tcp://localhost:48040/".
            Dim server1 = New EasyUAServer()


            ' The simplest way to construct the server object is to use the default constructor. The server will run on its
            ' constructor.
            Dim server2 = New EasyUAServer("opc.tcp://localhost:38444")


            ' The server object can also be constructed with multiple endpoint URL strings passed as an array to the
            ' constructor.
            Dim server3 = New EasyUAServer(
            {
                "opc.tcp://localhost:38444", "opc.tcp://localhost:38445", "opc.tcp://localhost:38446"
            })


            ' If the language supports variable number of arguments (such as C# or VB.NET), the multiple endpoint URL
            ' strings can be passed to it as separate arguments, instead of having to create an array of them.
            Dim server4 = New EasyUAServer(
                "opc.tcp://localhost:38444", "opc.tcp://localhost:38445", "opc.tcp://localhost:38446")


            ' The server object can be constructed with specific message security modes.
            Dim server5 = New EasyUAServer(UAMessageSecurityModes.Secure)


            ' The message security modes can be combined with the endpoint URL string.
            Dim server6 = New EasyUAServer(UAMessageSecurityModes.Secure, "opc.tcp://localhost:38444")


            ' The message security modes can also be combined with multiple endpoint URL strings in an array.
            Dim server7 = New EasyUAServer(UAMessageSecurityModes.Secure,
            {
                "opc.tcp://localhost:38444", "opc.tcp://localhost:38445", "opc.tcp://localhost:38446"
            })


            ' If the language supports variable number of arguments (such as C# or VB.NET), the message security modes can
            ' be combined with multiple endpoint URL strings passed to it as separate arguments, instead of having to create
            ' an array of them.
            Dim server8 = New EasyUAServer(
                UAMessageSecurityModes.Secure,
                "opc.tcp://localhost:38444", "opc.tcp://localhost:38445", "opc.tcp://localhost:38446")


            ' The endpoint can be specified using the Uri object.
            Dim server9 = New EasyUAServer(New Uri("opc.tcp://localhost:38444"))


            ' The server object can also be constructed with multiple Uri objects for server endpoints, passed as an array
            ' to the constructor.
            Dim server10 = New EasyUAServer(
            {
                New Uri("opc.tcp://localhost:38444"),
                New Uri("opc.tcp://localhost:38445"),
                New Uri("opc.tcp://localhost:38446")
            })


            ' If the language supports variable number of arguments (such as C# or VB.NET), the multiple endpoint Uri
            ' objects can be passed to it as separate arguments, instead of having to create an array of them.
            Dim server11 = New EasyUAServer(
                New Uri("opc.tcp://localhost:38444"),
                New Uri("opc.tcp://localhost:38445"),
                New Uri("opc.tcp://localhost:38446")
            )


            ' The message security modes can be combined with the endpoint Uri object.
            Dim server12 = New EasyUAServer(UAMessageSecurityModes.Secure, New Uri("opc.tcp://localhost:38444"))


            ' The message security modes can also be combined with multiple endpoint Uri objects in an array.
            Dim server13 = New EasyUAServer(UAMessageSecurityModes.Secure,
            {
                New Uri("opc.tcp://localhost:38444"),
                New Uri("opc.tcp://localhost:38445"),
                New Uri("opc.tcp://localhost:38446")
            })


            ' If the language supports variable number of arguments (such as C# or VB.NET), the message security modes can
            ' be combined with multiple endpoint Uri objects passed to it as separate arguments, instead of having to create
            ' an array of them.
            Dim server14 = New EasyUAServer(
                UAMessageSecurityModes.Secure,
                New Uri("opc.tcp://localhost:38444"),
                New Uri("opc.tcp://localhost:38445"),
                New Uri("opc.tcp://localhost:38446")
            )


            ' The message security modes and the endpoint URL string can be set after the server object is constructed.
            Dim server15 = New EasyUAServer()
            server15.MessageSecurityModes = UAMessageSecurityModes.Secure
            server15.EndpointUrlString = "opc.tcp://localhost:38444"


            ' If the language supports property initializers (such as C# or VB.NET), the above code can be written more
            ' concisely.
            Dim server16 = New EasyUAServer With {
                .MessageSecurityModes = UAMessageSecurityModes.Secure,
                .EndpointUrlString = "opc.tcp://localhost:38444"
            }


            ' If the language supports collection initializers (such as C# or VB.NET), the server object can be constructed
            ' with the contents of the Objects folder, such as the data variables, in a single statement.
            Dim server17 = New EasyUAServer From
            {
                New UADataVariable("Constant1").ConstantValue(42),
                New UADataVariable("Constant2").ConstantValue("abc")
            }

        End Sub
    End Class
End Namespace

Interfaces and Extension Methods

The main properties and methods that constitute the functionality of the EasyUAServer Class are actually implementations of IEasyUAServer Interface members.  The remaining methods and method overloads, which simply build upon the core interface methods, are implemented as extension methods on the IEasyUAServer Interface. Also, at many places, OPC Wizard uses arguments and properties of the IEasyUAServer Interface type, instead of the concrete EasyUAServer Class.

In most languages (certainly in C# and VB.NET), this design leads to the same syntax as if all the methods were implemented directly on the core concrete object.

Shared Instance

Instead of explicitly creating an instance of the EasyUAServer Class, you can also use a single, pre-made instance o fit, resulting in slightly shorter code. You can obtain it from the SharedInstance Property.

See Also

Reference

Examples - Server OPC Unified Architecture

Concepts